﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EventReminder.Common;

namespace EventReminder.Client
{
    internal partial class DayForm : Form
    {
        private EventReminderController _controller;

        public DateTime Day { get; set; }

        public DayForm()
            : this (null)
        { }

        public DayForm(EventReminderController eventReminderController)
        {
            this._controller = eventReminderController;

            // observer subscribe
            _controller.EventsUpdated += new EventHandler<EventsUpdatedArgs>(HandleEventsUpdated);
            
            InitializeComponent();
        }

        private void HandleEventsUpdated(object sender, EventsUpdatedArgs args)
        {
            // how can I throw a Virtual Proxy in here somewhere...?
            // Make a ListBoxVirtualProxy and have it display "loading events" until events are loaded

            List<EventData> events = new List<EventData>(args.Events);
            events.Sort();
            lstEvents.Items.Clear();
            foreach(EventData data in events)
            {
                lstEvents.Items.Add(data);
            }
        }

        private void HandleBtnNew_Click(object sender, EventArgs e)
        {
            _controller.OpenNewEvent(Day);
        }

        private void HandleBtnModify_Click(object sender, EventArgs e)
        {
            if (lstEvents.SelectedItem != null)
                _controller.OpenModifyEvent(lstEvents.SelectedItem as EventData);
        }

        private void HandleBtnDelete_Click(object sender, EventArgs e)
        {
            if (lstEvents.SelectedItem != null)
                _controller.DeleteSelectedEvent(lstEvents.SelectedItem as EventData);
        }

        private void HandleBtnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        protected override void OnClosed(EventArgs e)
        {
            // observer unsubscribe
            _controller.EventsUpdated -= new EventHandler<EventsUpdatedArgs>(HandleEventsUpdated);

            base.OnClosed(e);
        }
    }
}
